package com.handy.plugin.auth.action;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.database.DBUtil;
import com.handy.plugin.auth.bean.HAuthDyna;
import com.handy.plugin.auth.bean.HAuthTree;
import com.handy.plugin.auth.dao.HAuthDAO;
import com.handy.plugin.auth.dao.HAuthTreeDAO;

public class AuthTree {
 
	public int insert(int pid, String title, int kind, String note, String condition, String pcol, String vcol, String tablename){
		int ret = 0;
		HAuthTree bean = new HAuthTree();
		bean.setPid(pid);
		bean.setTitle(title);
		bean.setKind(kind);
		bean.setNote(note);
		bean = treeDAO.insert(bean);
		if (null == bean){
			ret = 2;
		}else{
			if (kind==1){
				HAuthDyna authDyna = new HAuthDyna();
				authDyna.setAuthid(bean.getId());
				authDyna.setCondition(condition);
				authDyna.setPcol(pcol);
				authDyna.setVcol(vcol);
				authDyna.setTname(tablename);
				boolean b = new HAuthDAO().insertAuthDyna(authDyna);
				if(!b) ret = 3;
			}
		}
		return ret;
	}
	
	public int update(int id, String title, int kind, String note, String condition, String pcol, String vcol, String tablename){
		int ret = 0;
		HAuthTree bean = new HAuthTree();
		bean.setId(id);
		bean.setTitle(title);
		bean.setKind(kind);
		bean.setNote(note);
		if (null == treeDAO.update(bean)){
			ret = 2;
		}else{
			if (kind==1){
				HAuthDyna authDyna = new HAuthDyna();
				authDyna.setAuthid(bean.getId());
				authDyna.setCondition(condition);
				authDyna.setPcol(pcol);
				authDyna.setVcol(vcol);
				authDyna.setTname(tablename);
				boolean b = new HAuthDAO().updateAuthDyna(authDyna);
				if(!b) ret = 3;
			}
		}
		return ret;
	}
	
	public int deletebyid(int id, int kind){
		int ret = 0;
		if (id > 100) {
			try {
				treeDAO.delete(id);
			} catch (SQLException e) {
				ret = 2;
				log.error(e.getMessage());
			}
		} else {
			ret = 3;
		}
		if (kind==1){
			new HAuthDAO().deleteAuthDyna(id);
		}
		return ret;
	}
	
	public int up(int id){
		int ret = 0;
		try {
			treeDAO.upPosition(id);
		} catch (SQLException e) {
			ret = 2;
			log.error(e.getMessage());
		}
		
		return ret;
	}
	
	public int down(int id){
		int ret = 0;
		try {
			treeDAO.downPosition(id);
		} catch (SQLException e) {
			ret = 2;
			log.error(e.getMessage());
		}
		return ret;
	}

	// mode=0 查找role; mode=1查找user
	// 通过roleid/userid查找v_auth_all,返回角色/用户所有的权限,
	// 并在结果集的hashmap加上<"vcolvalue",pcolvalue对应的值>
	@SuppressWarnings("unchecked")
	public ArrayList<Map<String, Object>> findAllAuth(int mode, int id) {
		String sql = "";
		if (0 == mode)
			sql = "select * from v_auth_all where roleid = " + id + " order by roleid";
		else
			sql = "select * from v_auth_all where userid = " + id + " order by userid";
		ArrayList<Map<String, Object>> rs = (ArrayList<Map<String, Object>>) dbUtil.find("", sql);
		if (rs != null) {
			for (Iterator iter = rs.iterator(); iter.hasNext();) {
				Map<String, Object> map = (Map<String, Object>) iter.next();
				String vcolvalue = null;
				String nodename = null;
				int pcolvalue = Integer.valueOf((String) map.get("pcolvalue"));
				int authid = Integer.valueOf(map.get("authid").toString());
				nodename = (String) map.get("treetilte");
				// pcolvalue=-1为静态权限; >0为扩展权限
				if (-1 != pcolvalue) {
					// 通过authid查找h_auth_dyna表,得到动态表结构
					sql = "select * from h_auth_dyna where authid = " + authid;
					Map<String, Object> dyna = dbUtil.findFirst("", sql);
					String tname = (String) dyna.get("tname");
					String vcol = (String) dyna.get("vcol");
					String pcol = (String) dyna.get("pcol");
					// 组装SQL
					sql = "select " + vcol + " from " + tname + " where "
							+ pcol + " = " + pcolvalue;
					// System.out.println("组装SQL= " + sql);
					vcolvalue = dbUtil.getSampleStringValue("", sql);
					if("".equals(vcolvalue)) vcolvalue = "未知扩展权限";
					nodename = nodename + " >> " + vcolvalue+"("+pcolvalue+")";
				}
				map.put("pcolvalue", pcolvalue);
				map.put("vcolvalue", vcolvalue);
				map.put("nodename", nodename);
			}
		}
		return rs;
	}
	
	private static final Log log = LogFactory.getLog(AuthTree.class);
	private HAuthTreeDAO treeDAO = new HAuthTreeDAO();
	private DBUtil dbUtil = DBUtil.getInstance();
	private String dataSourceName ;
	
	public String getDataSourceName() {
		return dataSourceName;
	}
	public void setDataSourceName(String dataSourceName) {
		this.dataSourceName = dataSourceName;
	}
	
	
	
}
